/*
 * Author: Honrun
 * brief:  电机驱动
 * Created on 2021年8月17日, 下午2:04
 */

#include "gd32f10x.h"
#include "stdint.h"
#include "stdio.h"
#include "DevicesMotor.h"



MotorInfoType g_typeMotorInfo = {0};


void vMotorOutputInit(void)
{
    timer_oc_parameter_struct timer_ocinitpara = {0};
    timer_parameter_struct timer_initpara = {0};

    /* enable the GPIO clock */
    rcu_periph_clock_enable(MOTOR_PWM_LEFT_MOVE_RCC_GPIO_CLK);
    rcu_periph_clock_enable(MOTOR_PWM_LEFT_BACK_RCC_GPIO_CLK);
    rcu_periph_clock_enable(MOTOR_PWM_RIGHT_MOVE_RCC_GPIO_CLK);
    rcu_periph_clock_enable(MOTOR_PWM_RIGHT_BACK_RCC_GPIO_CLK);

    /*configure TIMERx CHx as alternate function*/
    gpio_init(MOTOR_PWM_LEFT_MOVE_GPIO_Port, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, MOTOR_PWM_LEFT_MOVE_Pin);
    gpio_init(MOTOR_PWM_LEFT_BACK_GPIO_Port, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, MOTOR_PWM_LEFT_BACK_Pin);
    gpio_init(MOTOR_PWM_RIGHT_MOVE_GPIO_Port, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, MOTOR_PWM_RIGHT_MOVE_Pin);
    gpio_init(MOTOR_PWM_RIGHT_BACK_GPIO_Port, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, MOTOR_PWM_RIGHT_BACK_Pin);


    /* enable the TIMER clock */
    rcu_periph_clock_enable(RCU_TIMER3);
    /* deinit a TIMER */
    timer_deinit(TIMER3);
    /* initialize TIMER init parameter struct */
    timer_struct_para_init(&timer_initpara);
    /* TIMERx configuration */
    timer_initpara.prescaler         = MOTOR_PWM_PRESCALER - 1;
    timer_initpara.period            = MOTOR_PWM_PERIOD;
    timer_initpara.alignedmode       = TIMER_COUNTER_EDGE;
    timer_initpara.counterdirection  = TIMER_COUNTER_UP;
    timer_initpara.clockdivision     = TIMER_CKDIV_DIV1;
    timer_initpara.repetitioncounter = 0;
    timer_init(TIMER3, &timer_initpara);


    /* initialize TIMER channel output parameter struct */
    timer_channel_output_struct_para_init(&timer_ocinitpara);
    /* configure TIMER channel output function */
    timer_ocinitpara.outputstate  = TIMER_CCX_ENABLE;
    timer_ocinitpara.outputnstate = TIMER_CCXN_DISABLE;
    timer_ocinitpara.ocpolarity   = TIMER_OC_POLARITY_HIGH;
    timer_ocinitpara.ocnpolarity  = TIMER_OCN_POLARITY_HIGH;
    timer_ocinitpara.ocidlestate  = TIMER_OC_IDLE_STATE_LOW;
    timer_ocinitpara.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;
    timer_channel_output_config(TIMER3, TIMER_CH_0, &timer_ocinitpara);
    timer_channel_output_config(TIMER3, TIMER_CH_1, &timer_ocinitpara);
    timer_channel_output_config(TIMER3, TIMER_CH_2, &timer_ocinitpara);
    timer_channel_output_config(TIMER3, TIMER_CH_3, &timer_ocinitpara);

    /* CH0 configuration in PWM mode0, duty cycle 0% */
    timer_channel_output_pulse_value_config(TIMER3, TIMER_CH_0, 0);
    timer_channel_output_mode_config(TIMER3, TIMER_CH_0, TIMER_OC_MODE_PWM0);
    timer_channel_output_shadow_config(TIMER3, TIMER_CH_0, TIMER_OC_SHADOW_DISABLE);

    /* CH1 configuration in PWM mode0, duty cycle 0% */
    timer_channel_output_pulse_value_config(TIMER3, TIMER_CH_1, 0);
    timer_channel_output_mode_config(TIMER3, TIMER_CH_1, TIMER_OC_MODE_PWM0);
    timer_channel_output_shadow_config(TIMER3, TIMER_CH_1, TIMER_OC_SHADOW_DISABLE);

    /* CH1 configuration in PWM mode0, duty cycle 0% */
    timer_channel_output_pulse_value_config(TIMER3, TIMER_CH_2, 0);
    timer_channel_output_mode_config(TIMER3, TIMER_CH_2, TIMER_OC_MODE_PWM0);
    timer_channel_output_shadow_config(TIMER3, TIMER_CH_2, TIMER_OC_SHADOW_DISABLE);

    /* CH1 configuration in PWM mode0, duty cycle 0% */
    timer_channel_output_pulse_value_config(TIMER3, TIMER_CH_3, 0);
    timer_channel_output_mode_config(TIMER3, TIMER_CH_3, TIMER_OC_MODE_PWM0);
    timer_channel_output_shadow_config(TIMER3, TIMER_CH_3, TIMER_OC_SHADOW_DISABLE);


    /* auto-reload preload enable */
    timer_auto_reload_shadow_enable(TIMER3);
     /* TIMERx counter enable */
    timer_enable(TIMER3);


    MOTOR_DISABLE_ALL();
}

int8_t cMotorSetSpeed(uint8_t ucChannel, float fSpeed)
{
    int32_t iPulseWitch = 0;

    iPulseWitch = fSpeed * 0.01f * TIMER_CAR(TIMER3);

    /* 当输入端 INA，INB 无输入信号时，请勿将 INA 和 INB 引脚悬空，以避免在高温应用时由于 IC 本体 P/N 结存在微弱漏电流， */
    /* 该漏电流流过下拉电阻会导致悬空引脚的输入电位由低电位变高电位，造成电路输出错误的信号。 */
    if(ucChannel & MOTOR_OUTPUT_CHANNEL_LEFT)
    {
        if(iPulseWitch >= 0)
        {
            TIMER_CH2CV(TIMER3) = 0;
            TIMER_CH0CV(TIMER3) = iPulseWitch;
        }
        else
        {
            TIMER_CH0CV(TIMER3) = 0;
            TIMER_CH2CV(TIMER3) = iPulseWitch;
        }

        g_typeMotorInfo.left.direction = (iPulseWitch >= 0) ? MOTOR_DIRECTION_MOVE : MOTOR_DIRECTION_BACK;
        g_typeMotorInfo.left.speedSet = fSpeed;
    }

    if(ucChannel & MOTOR_OUTPUT_CHANNEL_RIGHT)
    {
        if(iPulseWitch >= 0)
        {
            TIMER_CH3CV(TIMER3) = 0;
            TIMER_CH1CV(TIMER3) = iPulseWitch;
        }
        else
        {
            TIMER_CH1CV(TIMER3) = 0;
            TIMER_CH3CV(TIMER3) = iPulseWitch;
        }

        g_typeMotorInfo.right.direction = (iPulseWitch >= 0) ? MOTOR_DIRECTION_MOVE : MOTOR_DIRECTION_BACK;
        g_typeMotorInfo.right.speedSet = fSpeed;
    }

    return 0;
}

float fMotorGetSpeed(uint8_t ucChannel)
{
    float fSpeed = 0;

    if(ucChannel & MOTOR_OUTPUT_CHANNEL_LEFT)
    {
        fSpeed = g_typeMotorInfo.left.speedGet;
    }
    else if(ucChannel & MOTOR_OUTPUT_CHANNEL_RIGHT)
    {
        fSpeed = g_typeMotorInfo.right.speedGet;
    }

    return fSpeed;
}

int8_t cMotorInfoUpdate(void)
{
    // vMoterUpdateSpeed();

    return 0;
}

MotorInfoType *ptypeMotorGetInfo(void)
{
    return &g_typeMotorInfo;
}
